package com.qdxwx.manage.controller.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.user.UserLoginService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.user.UserLogin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户(前台用户)Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/user/login")
public class UserLoginController extends BaseController {

    @Autowired
    private UserLoginService userLoginService;

    /**
     * 查询用户登录日志
     */
    @GetMapping("/list.html")
    public String UserLogin(HttpServletRequest request, ModelMap mmap) {
        long userId = TKConvert.toLong(request.getParameter("userId"));
        if(userId < 1) return error404;
        mmap.put("userId", userId);
        return "user/login-list";
    }

    /**
     * 查询用户登录日志列表
     */
    @PostMapping("/list.json")
    @Permissions("user:login:list")
    @ResponseBody
    public TableDataInfo UserLoginList(HttpServletRequest request) {
        long userId = TKConvert.toLong(request.getParameter("userId"));
        List<UserLogin> list = userLoginService.getList(userId, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(userLoginService.count(userId));
        return rspData;
    }


}
